package com.example.myapplication.Outpatientappointment;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.GetHospitalIdBean;
import com.example.myapplication.ShuJu.HospitalIdBean;
import com.example.myapplication.ShuJu.LibraryDetailsBean;
import com.example.myapplication.apter.ViewpageDateApter;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.net.URL;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HospitalDetailsActivity extends AppCompatActivity {

    private Button but_hospitalDetails;
    private Button but_guaghao;
    private ViewPager vp_hospital;
    private OkHttpClient okHttpClient;
    private String hospitalDetailsActivityID;
    private HospitalIdBean hospitalIdBean;
    private Handler handler;
    private TextView text_hostital;
    private GetHospitalIdBean getHospitalIdBean;
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital_details);
        Intent intent = getIntent();
        hospitalDetailsActivityID = intent.getStringExtra("HospitalDetailsActivityID");
        initview();
        sendbloak();
        //根据id发送
        sendIDgetViewpage();
        //获取详情文本
        sendgetDate();
        //进入挂号页面
        enterregister();


    }


    private void enterregister() {
        but_guaghao.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(HospitalDetailsActivity.this,RegisterActivity.class);
                        HospitalDetailsActivity.this.startActivity(intent);
                    }
                });
    }

    private void sendgetDate() {
        okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(TokenBean.IP+"/prod-api/api/hospital/hospital/"+hospitalDetailsActivityID)
                .addHeader("Authorization", TokenBean.token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json =response.body().string();
                getHospitalIdBean = gson.fromJson(json, GetHospitalIdBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        text_hostital.setText(Html.fromHtml(getHospitalIdBean.getData().getBrief()));
                    }
                });
            }
        });
    }

    private void sendIDgetViewpage() {
        okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(TokenBean.IP+"/prod-api/api/hospital/banner/list?hospitalId="+hospitalDetailsActivityID)
                .addHeader("Authorization", TokenBean.token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json =response.body().string();
                hospitalIdBean = gson.fromJson(json, HospitalIdBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        getViewpageDate();
                    }
                });
            }
        });
    }

    private void getViewpageDate() {
        ViewpageDateApter viewpageDateApter = new ViewpageDateApter(HospitalDetailsActivity.this,hospitalIdBean.getData());
        vp_hospital.setAdapter(viewpageDateApter);
        mhandler.sendEmptyMessageDelayed(0,3*1000);
    }

    private void sendbloak() {
        but_hospitalDetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HospitalDetailsActivity.this.finish();
            }
        });
    }

    private void initview() {
        but_hospitalDetails = findViewById(R.id.but_HospitalDetails);
        vp_hospital = findViewById(R.id.vp_hospital);
        text_hostital = findViewById(R.id.text_hostital);
        but_guaghao = findViewById(R.id.but_guaghao);
        handler = new Handler();
        gson = new Gson();
    }

    private Handler mhandler =  new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                int currentItem = vp_hospital.getCurrentItem();
                currentItem = (currentItem+1) % 3 ;
                vp_hospital.setCurrentItem(currentItem);
                mhandler.sendEmptyMessageDelayed(0,3*1000);
            }
        }
    };
}